Skip to main content

How do I use the only and except sections in a pipeline as code?

The only and except sections in a pipeline as code allow you to specify the conditions under which a job should be included or excluded from the pipeline execution. These sections provide fine-grained control over the execution of jobs based on various criteria, such as branch names, tags, or variables.

Here's how you can use the only and except sections in your pipeline as code:

only: The only section specifies the conditions under which a job should be included in the pipeline execution. You can define one or more conditions using different keywords:

Branches: You can specify branch names or patterns to include jobs for specific branches.

only:
- master
- /^feature/.*/ # Matches any branch starting with "feature/"

Tags: You can specify tag names to include jobs for tagged pipeline runs.

only:
- tags

Variables: You can specify variable conditions to include jobs based on the values of specific variables.

only:
variables:
- $CI_COMMIT_REF_NAME == "develop"

except: The except section specifies the conditions under which a job should be excluded from the pipeline execution. Similar to the only section, you can define conditions based on branches, tags, or variables.

except:
- tags
- $CI_COMMIT_REF_NAME == "feature/obsolete"

Using the only and except sections, you can customize the execution of jobs in your pipeline based on specific criteria. This allows you to control when and where certain jobs are executed, improving the efficiency and flexibility of your pipeline configuration.

Remember to carefully consider the conditions you specify in these sections to ensure that the right jobs are included or excluded as intended. Regularly test and validate your pipeline configuration to ensure it meets your requirements.